package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.nineoldandroids.view.ViewHelper;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.personal.fragment.AllCourseListFragment;
import com.zhongjian.technologycal.mall.activity.personal.fragment.AllOrderListFragment;
import com.zhongjian.technologycal.mall.activity.personal.fragment.DoingCourseListFragment;
import com.zhongjian.technologycal.mall.activity.personal.fragment.OverCourseListFragment;
import com.zhongjian.technologycal.mall.activity.personal.fragment.WaitAppraiseOrderListFragment;
import com.zhongjian.technologycal.mall.activity.personal.fragment.WaitCourseListFragment;
import com.zhongjian.technologycal.mall.activity.personal.fragment.WaitDeliveryOrderListFragment;
import com.zhongjian.technologycal.mall.activity.personal.fragment.WaitPaymentOrderListFragment;
import com.zhongjian.technologycal.mall.activity.personal.fragment.WaitSendDeliveryOrderListFragment;
import com.zhongjian.technologycal.mall.adapter.OrderAdapter;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.view.LazyViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wei on
 * 我的课程页面实现
 */

public class MineCourseActivity extends BaseActivity implements View.OnClickListener{
    public static final String CATEGORY_NAME = "com.nanchen.aiyagirl.module.category.CategoryFragment.CATEGORY_NAME";
    @BindView(R.id.tv_main_title)
    TextView course_tv_main_title;
    @BindView(R.id.course_viewPager_order)
    LazyViewPager course_viewPager_order;
    @BindView(R.id.course_main_indicate_line)
    View course_main_indicate_line;
    @BindView(R.id.course_tv_all)
    TextView course_tv_all;
    @BindView(R.id.course_tv_wait_pay)
    TextView course_tv_wait_pay;
    @BindView(R.id.course_tv_wait_send)
    TextView course_tv_wait_send;
    @BindView(R.id.course_tv_wait_get)
    TextView course_tv_wait_get;
    private int indicateLineWidth;
    //tab名字
    private String[] tabStr = {"全部","待开课","进行中","已结束"};
    private List<Fragment> mFragment = new ArrayList<>();

    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }
    @Override
    public void initPresenter() {
        mFragment.add(new AllCourseListFragment());
        mFragment.add(new WaitCourseListFragment());
        mFragment.add(new DoingCourseListFragment());
        mFragment.add(new OverCourseListFragment());
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        course_tv_main_title.setText("我的课程");
        int tabState = getIntent().getIntExtra("tabState",0);
        OrderAdapter adapter = new OrderAdapter(getSupportFragmentManager(),tabStr,mFragment);
        course_viewPager_order.setAdapter(adapter);
        int blue = getResources().getColor(R.color.blue);
        int black = getResources().getColor(R.color.title_color);
        //初始化底部viewpager指示线的宽度
        int screenWidth = getResources().getDisplayMetrics().widthPixels;//屏幕宽度
        indicateLineWidth = screenWidth / mFragment.size();
        course_main_indicate_line.getLayoutParams().width = indicateLineWidth;
        switch (tabState){
            case 1:
                course_viewPager_order.setCurrentItem(1);
                course_tv_wait_pay.setTextColor(blue);
                course_tv_all.setTextColor(black);
                course_tv_wait_send.setTextColor(black);
                course_tv_wait_get.setTextColor(black);
                //位移
                ViewHelper.setTranslationX(course_main_indicate_line,indicateLineWidth);
                break;
            case 2:
                course_viewPager_order.setCurrentItem(2);
                course_tv_wait_send.setTextColor(blue);
                course_tv_all.setTextColor(black);
                course_tv_wait_pay.setTextColor(black);
                course_tv_wait_get.setTextColor(black);
                //位移
                ViewHelper.setTranslationX(course_main_indicate_line,indicateLineWidth* 2);
                break;
            case 3:
                course_viewPager_order.setCurrentItem(3);
                course_tv_wait_get.setTextColor(blue);
                course_tv_all.setTextColor(black);
                course_tv_wait_pay.setTextColor(black);
                course_tv_wait_send.setTextColor(black);
                //位移
                ViewHelper.setTranslationX(course_main_indicate_line,indicateLineWidth*3);
                break;
            case 4:
                course_viewPager_order.setCurrentItem(4);
                course_tv_all.setTextColor(black);
                course_tv_wait_pay.setTextColor(black);
                course_tv_wait_send.setTextColor(black);
                course_tv_wait_get.setTextColor(black);
                //位移
                ViewHelper.setTranslationX(course_main_indicate_line,indicateLineWidth*4);
                break;
        }
        initViewListener1();

    }


    private void initViewListener1() {
        course_tv_all.setOnClickListener(this);
        course_tv_wait_pay.setOnClickListener(this);
        course_tv_wait_send.setOnClickListener(this);
        course_tv_wait_get.setOnClickListener(this);
        //viewpager改变监听
        course_viewPager_order.setOnPageChangeListener(new LazyViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int startX = position * indicateLineWidth;
                float translateX = positionOffset * indicateLineWidth;
                int endX = (int) (startX+translateX);
                //位移
                ViewHelper.setTranslationX(course_main_indicate_line, endX);
            }

            @Override
            public void onPageSelected(int position) {
                //切换标签
                int halfwhite = getResources().getColor(R.color.title_color);
                int blue = getResources().getColor(R.color.blue);
                //改变标签颜色
                course_tv_all.setTextColor(position == 0 ? blue:halfwhite);
                course_tv_wait_pay.setTextColor(position == 1 ? blue:halfwhite);
                course_tv_wait_send.setTextColor(position == 2 ? blue:halfwhite);
                course_tv_wait_get.setTextColor(position == 3 ? blue:halfwhite);
            }
        });
        super.initViewListener();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //全部
            case R.id.course_tv_all:
                course_viewPager_order.setCurrentItem(0);
                break;
            //待支付
            case R.id.course_tv_wait_pay:
                course_viewPager_order.setCurrentItem(1);
                break;
            //待发货
            case R.id.course_tv_wait_send:
                course_viewPager_order.setCurrentItem(2);
                break;
            //待收货
            case R.id.course_tv_wait_get:
                course_viewPager_order.setCurrentItem(3);
                break;

        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_course_list;
    }
    @OnClick(R.id.iv_back_page)
    public void onClickBack(){
        finish();
    }

}
